package com.lz.library.net

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager
import android.provider.Settings
import android.telephony.TelephonyManager
import androidx.annotation.MainThread
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.lz.library.extension.APP

/**
 *  author: you : 2020/6/17
 */
object NetworkManager : BroadcastReceiver() {

    private val observers = ArrayList<StateObserver>()

    var networkType: NetworkType? = null
        private set

    override fun onReceive(context: Context, intent: Intent) {
        if (ConnectivityManager.CONNECTIVITY_ACTION == intent.action) {
            val currentNetworkType = currentNetworkType()
            if (currentNetworkType != networkType) {
                networkType = currentNetworkType
                for (observer in observers) {
                    observer.onStateChanged(currentNetworkType)
                }
            }
        }
    }

    private fun currentNetworkType(): NetworkType {
        val cm = APP.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networkInfo = cm.activeNetworkInfo
        if (networkInfo != null && networkInfo.isAvailable) {
            return when (networkInfo.type) {
                ConnectivityManager.TYPE_WIFI -> NetworkType.WIFI
                TelephonyManager.NETWORK_TYPE_UNKNOWN -> NetworkType.UNKNOW
                else -> NetworkType.FLOW
            }
        }
        return NetworkType.NONE
    }

    internal fun register(context: Context) {
        networkType = currentNetworkType()
        context.registerReceiver(this, IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION))
    }

    internal fun unregister(context: Context) {
        context.unregisterReceiver(this)
    }

    fun isNetworkConnected(): Boolean = networkType != NetworkType.NONE

    fun isWifiConnected(): Boolean = networkType == NetworkType.WIFI

    fun openWirelessSettings() {
        APP.startActivity(Intent(Settings.ACTION_WIRELESS_SETTINGS).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
    }

    @MainThread
    fun observe(owner: LifecycleOwner, observer: StateObserver) {
        if (owner.lifecycle.currentState == Lifecycle.State.DESTROYED) return
        observers.add(observer)
        owner.lifecycle.addObserver(object : LifecycleEventObserver {
            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                if (event == Lifecycle.Event.ON_DESTROY) {
                    removeObserver(observer)
                }
            }
        })
    }

    /**
     * 注意当适当使用 @see [removeObserver]
     */
    @MainThread
    fun observeForever(observer: StateObserver) {
        observers.add(observer)
    }

    @MainThread
    fun removeObserver(observer: StateObserver) {
        observers.remove(observer)
    }

    interface StateObserver {

        fun onStateChanged(type: NetworkType)
    }
}